package com.scms.secondclass.service.impl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Set;
import java.util.HashSet;
import com.scms.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scms.secondclass.mapper.CreditRecordMapper;
import com.scms.secondclass.domain.CreditRecord;
import com.scms.secondclass.service.ICreditRecordService;

/**
 * 学分记录Service业务层处理
 * 
 * @author thy
 * @date 2025-03-06
 */
@Service
public class CreditRecordServiceImpl implements ICreditRecordService 
{
    @Autowired
    private CreditRecordMapper creditRecordMapper;

    /**
     * 查询学分记录
     * 
     * @param recordId 学分记录主键
     * @return 学分记录
     */
    @Override
    public CreditRecord selectCreditRecordByRecordId(Long recordId)
    {
        return creditRecordMapper.selectCreditRecordByRecordId(recordId);
    }

    /**
     * 查询学分记录列表
     * 
     * @param creditRecord 学分记录
     * @return 学分记录
     */
    @Override
    public List<CreditRecord> selectCreditRecordList(CreditRecord creditRecord)
    {
        return creditRecordMapper.selectCreditRecordList(creditRecord);
    }

    /**
     * 新增学分记录
     * 
     * @param creditRecord 学分记录
     * @return 结果
     */
    @Override
    public int insertCreditRecord(CreditRecord creditRecord)
    {
        creditRecord.setCreateTime(DateUtils.getNowDate());
        return creditRecordMapper.insertCreditRecord(creditRecord);
    }

    /**
     * 修改学分记录
     * 
     * @param creditRecord 学分记录
     * @return 结果
     */
    @Override
    public int updateCreditRecord(CreditRecord creditRecord)
    {
        creditRecord.setUpdateTime(DateUtils.getNowDate());
        return creditRecordMapper.updateCreditRecord(creditRecord);
    }

    /**
     * 批量删除学分记录
     * 
     * @param recordIds 需要删除的学分记录主键
     * @return 结果
     */
    @Override
    public int deleteCreditRecordByRecordIds(Long[] recordIds)
    {
        return creditRecordMapper.deleteCreditRecordByRecordIds(recordIds);
    }

    /**
     * 删除学分记录信息
     * 
     * @param recordId 学分记录主键
     * @return 结果
     */
    @Override
    public int deleteCreditRecordByRecordId(Long recordId)
    {
        return creditRecordMapper.deleteCreditRecordByRecordId(recordId);
    }
    
    /**
     * 获取用户学分统计
     * 
     * @param userId 用户ID
     * @return 学分统计信息
     */
    @Override
    public Map<String, Object> getUserCreditStats(Long userId)
    {
        Map<String, Object> stats = new HashMap<>();
        
        // 查询用户已通过的学分记录
        CreditRecord query = new CreditRecord();
        query.setUserId(userId);
        query.setStatus("1"); // 已通过
        List<CreditRecord> records = creditRecordMapper.selectCreditRecordList(query);
        
        // 计算总学分
        BigDecimal totalCredit = BigDecimal.ZERO;
        Map<String, BigDecimal> semesterCredits = new HashMap<>();
        
        for (CreditRecord record : records)
        {
            BigDecimal creditValue = record.getCreditValue();
            if (creditValue != null)
            {
                // 累加总学分
                totalCredit = totalCredit.add(creditValue);
                
                // 按学期统计
                String semester = record.getSemester();
                if (semester != null && !semester.isEmpty())
                {
                    BigDecimal semesterCredit = semesterCredits.getOrDefault(semester, BigDecimal.ZERO);
                    semesterCredit = semesterCredit.add(creditValue);
                    semesterCredits.put(semester, semesterCredit);
                }
            }
        }
        
        stats.put("totalCredit", totalCredit);
        stats.put("semesterCredits", semesterCredits);
        stats.put("recordCount", records.size());
        
        return stats;
    }
    
    /**
     * 获取学分统计总览数据
     * 
     * @return 学分统计总览数据
     */
    @Override
    public Map<String, Object> getCreditStatsOverview()
    {
        Map<String, Object> overview = new HashMap<>();
        
        // 查询所有已通过的学分记录
        CreditRecord query = new CreditRecord();
        query.setStatus("1"); // 已通过
        List<CreditRecord> allRecords = creditRecordMapper.selectCreditRecordList(query);
        
        // 计算总学分和当前学期学分
        BigDecimal totalCredits = BigDecimal.ZERO;
        BigDecimal currentSemesterCredits = BigDecimal.ZERO;
        String currentSemester = "2024-2025-2"; // 当前学期，实际应从配置或数据库获取
        
        // 用户ID集合，用于统计用户数量
        Set<Long> userIds = new HashSet<>();
        
        // 部门学分统计
        Map<String, BigDecimal> deptCredits = new HashMap<>();
        
        for (CreditRecord record : allRecords)
        {
            BigDecimal creditValue = record.getCreditValue();
            if (creditValue != null)
            {
                // 累加总学分
                totalCredits = totalCredits.add(creditValue);
                
                // 累加当前学期学分
                if (currentSemester.equals(record.getSemester()))
                {
                    currentSemesterCredits = currentSemesterCredits.add(creditValue);
                }
                
                // 记录用户ID
                if (record.getUserId() != null)
                {
                    userIds.add(record.getUserId());
                }
            }
        }
        
        // 计算平均学分
        BigDecimal averageCredits = BigDecimal.ZERO;
        if (!userIds.isEmpty())
        {
            averageCredits = totalCredits.divide(new BigDecimal(userIds.size()), 2, RoundingMode.HALF_UP);
        }
        
        // 设置统计结果
        overview.put("totalUsers", userIds.size());
        overview.put("totalCredits", totalCredits);
        overview.put("averageCredits", averageCredits);
        overview.put("currentSemesterCredits", currentSemesterCredits);
        
        return overview;
    }
}
